<?php

class Partners extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('mpartners');
        $this->load->library('session');
        $this->load->library('form_validation');
        $this->load->library('encrypt');
        $this->load->model('mmaster');
        $this->load->model('mglobal');
    }

    ########################################################################
    ########################################################################

    function migration() {
        
        $data['template'] = "shell/smooth";
        $data['main_view'] = 'f-partners';   
        $data['user_group']=$this->mmaster->getUserGroup($this->session->userdata('username'));    
        if($data['user_group']<>10){
            $result = $this->mmaster->getTerritoryPath( $this->session->userdata( 'username' ), 'cluster' );
    		$data[ 'cluster' ][ 'member_display' ] = $result[ 'path' ];
    		$data[ 'cluster' ][ 'member_value' ]   = $result[ 'territory_id' ];
    		}
        $data['status']='';
       if($this->input->post()){
           $i=0;
           
            if($this->input->post('destination')!='Unmaintained')
                $parent_id=$this->mpartners->getParentTerritory($this->input->post('destination'));
            else
                
                $parent_id=$this->mpartners->getParentTerritory($this->input->post('source'));
            foreach($this->input->post('territory_id') as $key=>$value)
            {
                if($this->input->post('destination')!='Unmaintained')
                    $dataMigration[$i]['user_id']=$this->input->post('destination');
                else
                    $dataMigration[$i]['user_id']=null;
                $dataMigration[$i]['parent_id']=$parent_id;
                $dataMigration[$i]['territory_id']=$key;
                $i++;
            }
            for($ii=0;$ii<$i;$ii++){
                $this->mpartners->migratePartners($dataMigration[$ii]);
                }
            $data['status']="Partners berhasil di migrasikan dari sales ".$this->input->post('source')." ke sales ".$this->input->post('destination');
        }
         $this->load->view($data['template'], $data);         
    }
    
    function getSales($user_group_id){
        echo json_encode($this->mpartners->getSales($user_group_id));        
    }

    function getPartners($user_group_id="",$user_id=""){
        echo json_encode($this->mpartners->getPartners($user_group_id,$user_id));
    }
    
    function getRegion(){
        echo json_encode($this->mpartners->getRegion());
    }
    
    function getCluster($region_id){
        if($region_id)
            echo json_encode($this->mpartners->getCluster($region_id));
    }
    
    function getSalesFromCluster($user_id,$user_group_id){
        echo json_encode($this->mpartners->getSalesFromCluster($user_id,$user_group_id));        
    }

    function getPartnersFromCluster($reporting_to,$user_group_id="",$user_id=""){
        echo json_encode($this->mpartners->getPartnersFromCluster($reporting_to,$user_group_id,$user_id));
    }
    
    
}